package com.edu.networkexperimentation.model.algorithm;

import com.edu.networkexperimentation.model.domain.Question;
import lombok.Data;

import java.util.List;


/**
 * 实现多种适应度函数的计算
 */
@Data
public class FitnessUtils {

//    static public Double getFitness(Individual individual, double pre_difficulty) {
//        double difficulty = individual.getDifficulty();
//        double fitness = 1 - Math.abs(difficulty - pre_difficulty);
//        fitness = Math.abs(fitness);
//
//
//        return fitness;
//    }


    static public Double getFitness(Individual individual, double pre_difficulty, List<Integer> rates) {
        double difficulty = individual.getDifficulty();
        double fitness = 1 - Math.abs(difficulty - pre_difficulty);
        fitness = Math.abs(fitness);

        fitness += getKnowledgeCoverage(individual, rates);
        return fitness;
    }

    static public double getKnowledgeCoverage(Individual individual, List<Integer> rates) {
        double knowledgeCoverage = 0;
        int[] nums = new int[4];
        int all_num = individual.getQuestions().size();
        double allRate = 0;
        for (int i = 0; i < rates.size(); i++) {
            nums[i] = 0;
            allRate += rates.get(i);
        }
        Double[] q_rates = new Double[rates.size()];
        for (int i = 0; i < rates.size(); i++) {
            q_rates[i] = 1.0 * rates.get(i) / allRate;
        }
        individual.getQuestions().forEach(
                item -> {
                    nums[item.getInter()]++;
                }
        );
        for (int i = 0; i < rates.size(); i++) {
            double t = 1.0 * nums[i] / all_num;
            if (Math.abs(q_rates[i]) <= 1e7) continue;
            knowledgeCoverage += t * q_rates[i];
        }
        return knowledgeCoverage;
    }
}
